package rbac.models;

import java.util.ArrayList;
import java.util.List;

import com.et.ar.ActiveRecordBase;
import com.et.ar.annotations.Column;
import com.et.ar.annotations.Id;
import com.et.ar.annotations.Table;

@Table(name = "Dutys")
public class Duty extends ActiveRecordBase {
	@Id
	private Integer id;
	@Column
	private Integer parentid;
	@Column
	private String name;
	@Column
	private Integer sort;

	public int getChildCount() throws Exception {
		return (int) count(Duty.class, "parentid=?", new Object[] { id });
	}

	public String findChildIds() throws Exception {
		String s = "";
		List<Integer> ids = new ArrayList<Integer>();
		ids.add(id);
		while (!ids.isEmpty()) {
			int id = ids.remove(0);
			s += id + ",";
			for (Duty type : Duty.findAll(Duty.class, "parentid=?",
					new Object[] { id })) {
				ids.add(type.getId());
			}
		}
		if (!s.equals("")) {
			s = s.substring(0, s.length() - 1);
		}
		return s;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getParentid() {
		return parentid;
	}

	public void setParentId(Integer parentid) {
		this.parentid = parentid;
	}

	public Integer getSort() {
		return sort;
	}

	public void setSort(Integer sort) {
		this.sort = sort;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

}
